$.strPad = function (i, l, s) {
    var o = i.toString();
    if (!s) {
        s = '0';
    }
    while (o.length < l) {
        o = s + o;
    }
    return o;
};

$(document).ready(function () {
    appBosch.date = new Date(appBosch.date);
    $('#updateTeams').click(function () {
        $.get(appBosch.updateMagicTeams, {}, function () {
            alert('Los equipos se han actualizado');
        });
    });
    $("#datepicker").kendoDatePicker({
        min: new Date('2015/01/01'),
        max: new Date(),
        value: appBosch.date,
        format: 'D',
        width: 300,
        change: function () {
            var dp = $("#datepicker").data('kendoDatePicker');
            var dt = dp.value();
            var year = dt.getFullYear();
            var month = $.strPad(dt.getMonth() + 1, 2);
            var date = $.strPad(dt.getDate(), 2);
            var dmy = [month, date, year].join('-');
            window.location.href = [appBosch.url, dmy].join('/');
        }
    });
    var currentDate = appBosch.date;
    var year = currentDate.getFullYear();
    var month = $.strPad(currentDate.getMonth() + 1, 2);
    var date = $.strPad(currentDate.getDate(), 2);
    var ymd = [year, month, date].join('-');
    var urlRead = [appBosch.crud.read, ymd].join('/');
    var dataSource = new kendo.data.DataSource({
        autoSync: true,
        transport: {
            read: {
                url: urlRead,
                dataType: "json"
            },
            create: {
                url: appBosch.crud.update,
                dataType: "json"
            },
            update: {
                url: appBosch.crud.update,
                dataType: "json"
            },
            parameterMap: function (options, operation) {
                var params = {};
                var dp = $("#datepicker").data('kendoDatePicker');
                var dt = dp.value();
                switch (operation) {
                    case 'create':
                        params = {models: kendo.stringify(options.models)};
                        break;
                    case 'read':
                        break;
                    case 'update':
                        options.models[0].wd = dt;
                        params = {models: kendo.stringify(options.models)};
                        console.log(params);
                        break;
                    case 'destroy':
                        params = {id: options.models[0].id};
                        break;
                }
                return params;
            }
        },
        batch: true,
        schema: {
            model: {
                id: "id",
                fields: {
                    u: {
                        editable: false,
                        nullable: false
                    },
                    nom: {
                        editable: false,
                        nullable: false
                    },
                    curp: {
                        editable: false,
                        nullable: false
                    },
                    full_name: {
                        editable: false,
                        nullable: false
                    },
                    l: {
                        editable: true,
                        nullable: true,
                        type: "string",
                        defaulValue: appBosch.lines[0].value,
                        width: 150
                    },
                    s: {
                        field: 's',
                        editable: true,
                        nullable: true,
                        type: "string",
                        defaulValue: 1
                    },
                    a: {
                        editable: true,
                        nullable: true,
                        type: "string",
                        defaulValue: appBosch.assists[0].value,
                        width: 150
                    }
                }
            }
        }
    });
    var widthCol = 120;
    var rowTemplate = kendo.template($("#row-template").html());
    $("#grid").kendoGrid({
        dataSource: dataSource,
        filterable: {
            mode: "row"
        },
        reorderable: true,
        resizable: true,
        columnMenu: true,
        sortable: true,
        columns: [
            {
                field: 'lc',
                title: '&nbsp;',
                template: '<div style="background-color: #=lc#; width: 40px; height: 20px;"></div>',
                width: 40,
                filterable: false
            },
            {
                field: "nom",
                title: "Nomina",
                width: 150
            },
            {
                field: "curp",
                title: "CURP",
                width: 200,
                hidden: true
            },
            {
                field: "full_name",
                title: "Nombre",
                width: 250
            },
            {
                field: "l",
                title: "Línea",
                width: 300,
                values: appBosch.lines
            },
            {
                field: "s",
                title: "Turno",
                width: 200,
                values: appBosch.shifts
            },
            {
                field: "a",
                title: "Acción",
                width: 200,
                values: appBosch.assists
            }
        ],
        editable: true
    });
});

function headerDate() {
    var str = kendo.toString(appBosch.date, 'D');
    appBosch.date.setDate(appBosch.date.getDate() + 1);
    return str;
}